package com.xinke.module.system.service.social;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.xinke.module.system.model.vo.social.*;
import com.xinke.module.system.model.entity.social.SocialUserDO;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.util.object.BeanUtils;

import com.xinke.module.system.mapper.social.SocialUserMapper;

import static com.xinke.component.common.exception.util.ServiceExceptionUtil.exception;
import static com.xinke.module.system.enums.ErrorCodeConstants.*;

/**
 * 社交用户 Service 实现类
 *
 * @author yzx
 */
@Service
@Validated
public class SocialUserServiceImpl implements SocialUserService {

    @Resource
    private SocialUserMapper socialUserMapper;

    @Override
    public Long createSocialUser(SocialUserSaveReqVO createReqVO) {
        // 插入
        SocialUserDO socialUser = BeanUtils.toBean(createReqVO, SocialUserDO.class);
        socialUserMapper.insert(socialUser);
        // 返回
        return socialUser.getSocialUserId();
    }

    @Override
    public void updateSocialUser(SocialUserSaveReqVO updateReqVO) {
        // 校验存在
        validateSocialUserExists(updateReqVO.getSocialUserId());
        // 更新
        SocialUserDO updateObj = BeanUtils.toBean(updateReqVO, SocialUserDO.class);
        socialUserMapper.updateById(updateObj);
    }

    @Override
    public void deleteSocialUser(Long socialUserId) {
        // 校验存在
        validateSocialUserExists(socialUserId);
        // 删除
        socialUserMapper.deleteById(socialUserId);
    }

    private void validateSocialUserExists(Long socialUserId) {
        if (socialUserMapper.selectById(socialUserId) == null) {
            throw exception(null);
        }
    }

    @Override
    public SocialUserDO getSocialUser(Long socialUserId) {
        return socialUserMapper.selectById(socialUserId);
    }

    @Override
    public PageResult<SocialUserDO> getSocialUserPage(SocialUserPageReqVO pageReqVO) {
        return socialUserMapper.selectPage(pageReqVO);
    }

}